package com.example.gameapplication.activity;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import com.example.gameapplication.R;
import com.example.gameapplication.ScreenUtils;

import java.util.List;

public class WindowActivity extends AppCompatActivity {

    private View mShowView;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mFloatParams;

    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_window);
        requestOverlayPermission();//权限
        setTopApp(this);

    }

    @Override
    protected void onStart() {
        super.onStart();
        dismiss();
    }


    @Override
    protected void onPause() {
        super.onPause();
        showFloatingWindowView(this, initFloatView());
    }

    public boolean requestOverlayPermission() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (!Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, 123);
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
    public void showFloatingWindowView(Context context, View view) {
        // 悬浮窗显示视图
        mShowView = view;
        // 获取系统窗口管理服务
        mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        // 悬浮窗口参数设置及返回
        mFloatParams = getParams();
        // 设置窗口触摸移动事件
        mShowView.setOnTouchListener(new FloatViewMoveListener());
        // 悬浮窗生成
        mWindowManager.addView(mShowView, mFloatParams);
    }
//    WindowManager.LayoutParams参数设置，主要设置悬浮窗口类型为WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY：
    private WindowManager.LayoutParams getParams() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        //设置悬浮窗口类型
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        } else {
            layoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
        }
        //设置悬浮窗口属性
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
                | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH;
        //设置悬浮窗口透明
        layoutParams.format = PixelFormat.TRANSLUCENT;
        //设置悬浮窗口长宽数据
        layoutParams.width = ScreenUtils.getScreenWidth(this)/10*6;
        layoutParams.height = ScreenUtils.getScreenHeight(this)/10*6;
        //设置悬浮窗显示位置
        layoutParams.gravity = Gravity.START | Gravity.TOP;
        layoutParams.x = 100;
        layoutParams.y = 100;
        return layoutParams;
    }
    private View initFloatView() {
        View view = View.inflate(this, R.layout.activity_main, null);
        //可以在这里给view的子控件设置点击事件
        return view;
    }

    private class FloatViewMoveListener implements View.OnTouchListener {

        //开始触控的坐标，移动时的坐标（相对于屏幕左上角的坐标）
        private int mTouchStartX;
        private int mTouchStartY;
        //开始时的坐标和结束时的坐标（相对于自身控件的坐标）
        private int mStartX, mStartY;
        //判断悬浮窗口是否移动，这里做个标记，防止移动后松手触发了点击事件
        private boolean isMove;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getAction();
            int x = (int) motionEvent.getX();
            int y = (int) motionEvent.getY();

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    isMove = false;
                    mTouchStartX = (int) motionEvent.getRawX();
                    mTouchStartY = (int) motionEvent.getRawY();
                    mStartX = x;
                    mStartY = y;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (motionEvent.getPointerCount()>2){

                    }
                    int mTouchCurrentX = (int) motionEvent.getRawX();
                    int mTouchCurrentY = (int) motionEvent.getRawY();
                    mFloatParams.x += mTouchCurrentX - mTouchStartX;
                    mFloatParams.y += mTouchCurrentY - mTouchStartY;
                    mWindowManager.updateViewLayout(mShowView, mFloatParams);
                    mTouchStartX = mTouchCurrentX;
                    mTouchStartY = mTouchCurrentY;
                    float deltaX = x - mStartX;
                    float deltaY = y - mStartY;
                    if (Math.abs(deltaX) >= 5 || Math.abs(deltaY) >= 5) {
                        isMove = true;
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                default:
                    break;
            }
            //如果是移动事件不触发OnClick事件，防止移动的时候一放手形成点击事件
            return isMove;
        }
    }
//    关闭悬浮窗，调用WindowManager的removeView方法即可：
    public void dismiss() {
        if (mWindowManager != null && mShowView != null) {
            mWindowManager.removeView(mShowView);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        dismiss();
    }

    @RequiresApi(api = Build.VERSION_CODES.Q)
    public void setTopApp(Context context) {
        //获取ActivityManager
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        //获得当前运行的task(任务)
        List<ActivityManager.RunningTaskInfo> taskInfoList = null;
        if (activityManager != null) {
            taskInfoList = activityManager.getRunningTasks(100);
        }
        if (taskInfoList != null) {
            for (ActivityManager.RunningTaskInfo taskInfo : taskInfoList) {
                //找到本应用的 task，并将它切换到前台
                if (taskInfo.topActivity != null && taskInfo.topActivity.getPackageName().equals(context.getPackageName())) {
                    activityManager.moveTaskToFront(taskInfo.id, 0);
                    break;
                }
            }
        }
    }
//    @Override
//    public void onAttachedToWindow() {
//        super.onAttachedToWindow();
//        Window window = getWindow();
//        final View view = window.getDecorView();
//        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
//        final WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();
////        lp.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;//无触摸事件
//        lp.gravity = Gravity.CENTER;
//        lp.width = ScreenUtils.getScreenWidth(this) / 8 * 6;
//        lp.height = ScreenUtils.getScreenHeight(this) / 8 * 6;
//        getWindowManager().updateViewLayout(view, lp);
//    }
}
